/**
 * 颜色生成工具
 * 根据主题色动态生成衍生颜色
 */

// 将十六进制颜色转换为HSL
function hexToHsl(hex: string): [number, number, number] {
  const r = parseInt(hex.slice(1, 3), 16) / 255;
  const g = parseInt(hex.slice(3, 5), 16) / 255;
  const b = parseInt(hex.slice(5, 7), 16) / 255;

  const max = Math.max(r, g, b);
  const min = Math.min(r, g, b);
  let h = 0;
  let s = 0;
  const l = (max + min) / 2;

  if (max !== min) {
    const d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);

    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0);
        break;
      case g:
        h = (b - r) / d + 2;
        break;
      case b:
        h = (r - g) / d + 4;
        break;
    }
    h /= 6;
  }

  return [h * 360, s * 100, l * 100];
}

// 将HSL转换为十六进制颜色
function hslToHex(h: number, s: number, l: number): string {
  h = h / 360;
  s = s / 100;
  l = l / 100;

  const hue2rgb = (p: number, q: number, t: number) => {
    if (t < 0) t += 1;
    if (t > 1) t -= 1;
    if (t < 1/6) return p + (q - p) * 6 * t;
    if (t < 1/2) return q;
    if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
    return p;
  };

  let r, g, b;

  if (s === 0) {
    r = g = b = l; // achromatic
  } else {
    const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
    const p = 2 * l - q;
    r = hue2rgb(p, q, h + 1/3);
    g = hue2rgb(p, q, h);
    b = hue2rgb(p, q, h - 1/3);
  }

  const toHex = (c: number) => {
    const hex = Math.round(c * 255).toString(16);
    return hex.length === 1 ? '0' + hex : hex;
  };

  return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}

// 调整颜色亮度
export function adjustLightness(hex: string, adjustment: number): string {
  const [h, s, l] = hexToHsl(hex);
  const newL = Math.max(0, Math.min(100, l + adjustment));
  return hslToHex(h, s, newL);
}

// 调整颜色饱和度
export function adjustSaturation(hex: string, adjustment: number): string {
  const [h, s, l] = hexToHsl(hex);
  const newS = Math.max(0, Math.min(100, s + adjustment));
  return hslToHex(h, newS, l);
}

// 生成衍生颜色
export interface DerivedColors {
  light: string;      // 浅色 - 对应 #FFEEA9
  medium: string;     // 中色 - 对应 #FEBA9C  
  accent: string;     // 强调色 - 对应 #FD9849
}

export function generateDerivedColors(primaryColor: string): DerivedColors {
  const [h, s, l] = hexToHsl(primaryColor);
  
  // 根据你提供的颜色分析规律：
  // #FFEEA9 - 很浅的暖黄色调
  // #FEBA9C - 中等的暖橙色调
  // #FD9849 - 较深的橙色调
  
  // 生成浅色：提高亮度，降低饱和度
  const light = hslToHex(
    Math.max(0, h - 10), // 色相稍微偏向黄色
    Math.max(20, s - 30), // 降低饱和度
    Math.min(95, l + 35)  // 提高亮度
  );
  
  // 生成中色：稍微调整亮度和饱和度
  const medium = hslToHex(
    Math.max(0, h - 5),   // 色相稍微调整
    Math.max(30, s - 15), // 稍微降低饱和度
    Math.min(85, l + 15)  // 稍微提高亮度
  );
  
  // 生成强调色：保持或稍微加深
  const accent = hslToHex(
    h,                    // 保持原色相
    Math.min(100, s + 5), // 稍微提高饱和度
    Math.max(10, l - 5)   // 稍微降低亮度
  );

  return {
    light,
    medium,
    accent
  };
}

// 生成渐变色组合
export function generateGradientColors(primaryColor: string): {
  background: string[];
  shadow: string[];
  decoration: string[];
} {
  const derived = generateDerivedColors(primaryColor);
  const [h, s, l] = hexToHsl(primaryColor);
  
  // 背景渐变色
  const background = [
    '#ffffff',
    derived.light,
    derived.medium,
    primaryColor
  ];
  
  // 阴影渐变色
  const shadow = [
    hslToHex(h, Math.max(10, s - 40), Math.min(98, l + 40)), // 极浅
    hslToHex(h, Math.max(15, s - 30), Math.min(95, l + 30)), // 很浅
    hslToHex(h, Math.max(20, s - 20), Math.min(90, l + 20)), // 浅
    hslToHex(h, Math.max(25, s - 10), Math.min(85, l + 10))  // 中浅
  ];
  
  // 装饰色
  const decoration = [
    derived.light,
    derived.medium,
    derived.accent,
    primaryColor
  ];
  
  return {
    background,
    shadow,
    decoration
  };
}

// 为特定主题色生成完整的颜色方案
export function generateThemeColorScheme(primaryColor: string) {
  const derived = generateDerivedColors(primaryColor);
  const gradients = generateGradientColors(primaryColor);
  
  return {
    primary: primaryColor,
    derived,
    gradients,
    // 生成透明度变体
    withOpacity: {
      primary: (opacity: number) => `${primaryColor}${Math.round(opacity * 255).toString(16).padStart(2, '0')}`,
      light: (opacity: number) => `${derived.light}${Math.round(opacity * 255).toString(16).padStart(2, '0')}`,
      medium: (opacity: number) => `${derived.medium}${Math.round(opacity * 255).toString(16).padStart(2, '0')}`,
      accent: (opacity: number) => `${derived.accent}${Math.round(opacity * 255).toString(16).padStart(2, '0')}`
    }
  };
}